﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Coding4Fun.Cannon.Phone.Controls;
using Microsoft.Phone.Controls;

namespace Coding4Fun.Cannon.Phone
{
	public partial class MainPage : PhoneApplicationPage
	{
		// Constructor
		public MainPage()
		{
			InitializeComponent();
			LayoutRoot.Visibility = Visibility.Collapsed;
		}

		UserControl _oldControl;
		UserControl _currentControl;

		//TODO: Clean this code up later
		void navigationControl1_ButtonNavigationChanged(object sender, ButtonNavigationEventArgs e)
		{
			StopAccelerometer();

			switch (e.NewButton)
			{
				case ButtonNavigation.Status:
					SetCurrentView(statusView1, StatusViewDown);
					break;
				case ButtonNavigation.Drive:
					SetCurrentView(driveView1, DriveViewDown);
					driveView1.StartAccelerometer();
					break;
				case ButtonNavigation.Aim:
					SetCurrentView(aimView1, AimViewDown);
					aimView1.StartAccelerometer();
					break;
				case ButtonNavigation.Fire:
					SetCurrentView(fireView1, FireViewDown);
					break;
				case ButtonNavigation.None:
					break;
				default:
					break;
			}
		}

		private void SetCurrentView(UserControl view, Storyboard animation)
		{
			if (IsCurrent(view))
				return;

			CheckMargin(view);

			SetCurrentControls(view);
			animation.Begin();
		}

		private static void CheckMargin(FrameworkElement control)
		{
			if (control == null)
				return;

			if (control.Margin.Top == 0)
				control.Margin = new Thickness(0, -480, 0, 0);
		}

		private void SetCurrentControls(UserControl newControl)
		{
			_oldControl = _currentControl;
			Canvas.SetZIndex(_oldControl, 0);
			newControl.Visibility = Visibility.Visible;
			_currentControl = newControl;
			Canvas.SetZIndex(newControl, 1);
		}

		private bool IsCurrent(UserControl control)
		{
			return control.GetType() == _currentControl.GetType();
		}

		private void StopAccelerometer()
		{
			//Stop
			App.Accelerometer.Stop();

			//unsubscribe from events
			driveView1.StopAccelerometer();
			aimView1.StopAccelerometer();

		}

		private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
		{
			LayoutRoot.Visibility = Visibility.Visible;

			AddStoryboardEventHandlers();

			_currentControl = statusView1;
			StatusViewDown.Begin();

		}

		private void AddStoryboardEventHandlers()
		{
			StatusViewDown.Completed += Animation_Completed;
			AimViewDown.Completed += Animation_Completed;
			DriveViewDown.Completed += Animation_Completed;
			StatusViewDown.Completed += Animation_Completed;
		}

		void Animation_Completed(object sender, EventArgs e)
		{
			if (_oldControl == null)
				return;

			SetZOrder(_oldControl, 0);
			_oldControl.Margin = new Thickness(0, -480, 0, 0);
			_oldControl.Visibility = Visibility.Collapsed;
		}

		private static void SetZOrder(UIElement control, int zOrder)
		{
			if (control == null)
				return;

			Canvas.SetZIndex(control, zOrder);
		}
	}
}
